﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Galaktika.BI.Silverlight.Controls.PivotGrid.Data;
using Galaktika.BI.Silverlight.Controls.General;
using Galaktika.BI.Charting.PivotChart;
using Galaktika.BI.Charting;
using Galaktika.BI.Extensibility.Dom;
using Galaktika.BI.Runtime;
using Galaktika.BI.Runtime.Services;

namespace Galaktika.BI.Silverlight.Controls.PivotGrid.Controls
{
    internal class SpanInfo
    {
        public readonly MemberControl Ctrl = null;
        public readonly int StartIndex = -1;
        public GridSplitter Splitter = null;

        public SpanInfo(MemberControl ctrl, int startIndex)
        {
            Ctrl = ctrl;
            StartIndex = startIndex;
        }
    }

    public abstract partial class MembersAreaContol : UserControl
    {
        public const int SPLITTER_SIZE = 3;
        public const double MIN_WIDTH = 20;
        public const double MIN_HEIGHT = 20;
        public const double DRILLDOWN_SPACE_HEIGHT = 10;
        public const double DRILLDOWN_SPACE_WIDTH = 10;
        public readonly GridLength Default_Height = GridLength.Auto;
        public readonly GridLength Default_Width = GridLength.Auto;

        public MembersAreaContol(PivotGridPanel grid)
        {
            Default_Height = new GridLength(22);
            Default_Width = new GridLength(120);

            if (grid == null)
                throw new ArgumentNullException("grid");
            PivotGrid = grid;

            InitializeComponent();

            this.GotFocus += new RoutedEventHandler(SpanMembersAreaContol_GotFocus);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(MembersAreaContol_MouseLeftButtonDown);
        }

        private CustomList<ActionInfo> m_ContextActionInfo = null;
        /// <summary>
        /// Контекстные действия
        /// </summary>
        public CustomList<ActionInfo> ContextActionInfo
        {
            get
            {
                return m_ContextActionInfo;
            }
            set
            {
                m_ContextActionInfo = value;
            }
        }

        void MembersAreaContol_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
        }

        void SpanMembersAreaContol_GotFocus(object sender, RoutedEventArgs e)
        {
            
        }

        protected readonly PivotGridPanel PivotGrid = null;

        public virtual void Initialize(MemberInfoCollection members)
        {
            LovestMemberControls.Clear();
        }

        IList<MemberControl> m_LovestMemberControls = null;
        public IList<MemberControl> LovestMemberControls
        {
            get
            {
                if (m_LovestMemberControls == null)
                {
                    m_LovestMemberControls = new List<MemberControl>();
                }
                return m_LovestMemberControls;
            }
        }

        public event MemberActionEventHandler DrillDownMember;
        protected void Raise_DrillDownMember(MemberActionEventArgs args)
        {
            MemberActionEventHandler handler = this.DrillDownMember;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        protected void OnDrillDownMember(object sender, MemberActionEventArgs args)
        {
            Raise_DrillDownMember(args);
        }

        CustomContextMenu m_ContextMenu = null;
        public CustomContextMenu ContextMenu
        {
            get {
                if (m_ContextMenu == null)
                {
                    CreateContextMenu();
                }
                return m_ContextMenu;
            }
        }

        private void CreateContextMenu()
        {
            m_ContextMenu = new CustomContextMenu();
            ContextMenuItem item;

            item = new ContextMenuItem("Expand");
            item.Tag = MemberActionType.Expand;
            m_ContextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(item_ItemClick);

            item = new ContextMenuItem("Collapse");
            item.Tag = MemberActionType.Collapse;
            m_ContextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(item_ItemClick);

            item = new ContextMenuItem("DrillDown");
            item.Tag = MemberActionType.DrillDown;
            m_ContextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(item_ItemClick);

            if (ContextActionInfo != null && ContextActionInfo.Count > 0)
            {
                if (m_ContextMenu.Items.Count > 0)
                {
                    m_ContextMenu.AddMenuSplitter();
                }

                foreach (ActionInfo info in ContextActionInfo)
                {
                    item = new ContextMenuItem(info.ACaption);
                    item.ItemClick += new EventHandler(item_ItemClick);
                    item.Tag = info;
                    item.Icon = UriResources.Images.ActionNode16;
                    m_ContextMenu.AddMenuItem(item);
                }
            }
        }


        void item_ItemClick(object sender, EventArgs e)
        {
            ContextMenuItem item = sender as ContextMenuItem;
            if (item != null && item.Tag != null)
            {
                if (item.Tag is ActionInfo)
                {
                    ActionInfo ai = item.Tag as ActionInfo;
                    if (ai != null && ai.AActionInvoker != null && ((BrickActionInvoker)ai.AActionInvoker).Action != null)
                    {
                        List<ArgumentEntry> args = new List<ArgumentEntry>(((BrickActionInvoker)ai.AActionInvoker).Parameters);

                        MemberInfo mi = ContextMenu.Tag as MemberInfo;
                        if (mi != null)
                        {
                            args.Add(new ArgumentEntry(mi.HierarchyUniqueName, mi.UniqueName));
                        }

                        ScriptEngine.Execute(new MockCaller(this.PivotGrid.SpanPivotGrid.Owner.Context), ((BrickActionInvoker)ai.AActionInvoker).Action, args);
                    }
                    return;
                }
                if (item.Tag is MemberActionType)
                {
                    MemberInfo mi = ContextMenu.Tag as MemberInfo;
                    if (mi != null)
                    {
                        Raise_DrillDownMember(new MemberActionEventArgs(mi, (MemberActionType)(item.Tag)));
                    }
                    return;
                }
            }
        }

        protected void OnShowContextMenu(object sender, MemberClickEventArgs args)
        {
            if (ContextMenu.IsDropDownOpen)
                ContextMenu.IsDropDownOpen = false;

            ContextMenu.SetLocation(args.Position);
            ContextMenu.Tag = args.Member;
            ContextMenu.IsDropDownOpen = true;
        }

        /// <summary>
        /// Добавляет в грид сплиттер, который меняет ширину
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="columnIndex"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        protected GridSplitter Add_VertSplitter(Grid grid, int columnIndex, int rowIndex, int Span)
        {
            GridSplitter splitter_Vert = new GridSplitter();
            splitter_Vert.IsTabStop = false;
            grid.Children.Add(splitter_Vert);
            Grid.SetColumn(splitter_Vert, columnIndex);
            Grid.SetRow(splitter_Vert, rowIndex);
            Grid.SetRowSpan(splitter_Vert, Span);

            splitter_Vert.HorizontalAlignment = HorizontalAlignment.Right;
            splitter_Vert.VerticalAlignment = VerticalAlignment.Stretch;
            splitter_Vert.Width = MembersAreaContol.SPLITTER_SIZE;

            /*GradientStopCollection stops = new GradientStopCollection();
            GradientStop stop0 = new GradientStop();
            stop0.Color = Colors.White;
            GradientStop stop1 = new GradientStop();
            stop1.Color = Colors.DarkGray;
            stop1.Offset = 1;
            stops.Add(stop0);
            stops.Add(stop1);
            splitter_Vert.Background = new LinearGradientBrush(stops, 0);*/
            splitter_Vert.Background = new SolidColorBrush(Colors.Transparent);

            return splitter_Vert;
        }

        /// <summary>
        /// Добавляет в грид сплиттер, который меняет высоту
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="columnIndex"></param>
        /// <param name="rowIndex"></param>
        /// <returns></returns>
        protected GridSplitter Add_HorzSplitter(Grid grid, int columnIndex, int rowIndex, int Span)
        {
            GridSplitter splitter_Horz = new GridSplitter();
            splitter_Horz.IsTabStop = false;
            grid.Children.Add(splitter_Horz);
            Grid.SetColumn(splitter_Horz, columnIndex);
            Grid.SetRow(splitter_Horz, rowIndex);
            Grid.SetColumnSpan(splitter_Horz, Span);

            splitter_Horz.HorizontalAlignment = HorizontalAlignment.Stretch;
            splitter_Horz.VerticalAlignment = VerticalAlignment.Bottom;
            splitter_Horz.Height = MembersAreaContol.SPLITTER_SIZE;

            splitter_Horz.Background = new SolidColorBrush(Colors.Transparent);

            return splitter_Horz;
        }
    }
}

